package com.sservice.web.controller.admin;

import com.sservice.common.Constants;
import com.sservice.model.vo.NoticeVo;
import com.sservice.urp.vo.UserVo;
import com.sservice.service.service.INoticeService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.Notice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/notice")
public class NoticeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private INoticeService noticeService;

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query/{status}/{readStatus}", method = RequestMethod.GET)
    public ListResult query(NoticeVo arg, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute(Constants.USER);
        arg.setReceiver(String.valueOf(user.getId()));
        if ("2".equals(arg.getReadStatus())) {
            arg.setReadStatus(null);
        }
        List<NoticeVo> list = noticeService.queryByPage(arg);
        int total = noticeService.queryByCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/queryStatis/{status}/{readStatus}", method = RequestMethod.GET)
    public BaseResult queryStatis(NoticeVo arg, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute(Constants.USER);
        arg.setReceiver(String.valueOf(user.getId()));

        int total = noticeService.queryByCount(arg);
        return new BaseResult(true, total);
    }

    @ResponseBody
    @RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
    public BaseResult read(Notice arg, HttpSession session) {
        arg.setReadStatus("1"); //
        int total = noticeService.updateByPrimaryKeySelective(arg);
        return new BaseResult(true, total);
    }
}
